<template>
  <div v-loading="loading">
    <el-form ref="form" :model="form" :rules="rules" label-width="120px" class="dataForm">
      <el-row>
        <el-col :span="12">
          <el-form-item :label="$t('出资人类型')" prop="type">
            <el-select
              v-model="form.type"
              :placeholder="$t('出资人类型')"
              filterable
              :disabled="form.id !== undefined && form.id !== null && form.id !== ''"
              @change="changeNameByTypeToSelect(form.type)"
            >
              <el-option
                v-for="dict in customerTypeOptions"
                :key="dict.dictValue"
                :label="$t(dict.dictLabel)"
                :value="dict.dictValue"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col v-if="form.type === 'GR' || form.type === 'JG'" :span="12">
          <el-form-item :label="$t('出资人名称')" prop="investName" class="lp-class">
            <el-select v-model="form.investName" clearable filterable :placeholder="$t('出资人名称')" :disabled="form.id !== undefined && form.id !== null && form.id !== ''" @change="nameSelect">
              <el-option
                v-for="item in nameOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
            <el-button
              @click="addCustomer"
            >{{ $t('添加客户') }}
            </el-button>
          </el-form-item>
        </el-col>
        <el-col v-else :span="12">
          <el-form-item :label="$t('出资人名称')" prop="investName">
            <el-select v-model="form.investName" clearable filterable :placeholder="$t('公司全称')" :disabled="form.id !== undefined && form.id !== null && form.id !== ''" @change="nameSelect">
              <el-option
                v-for="item in nameOptions"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              />
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('合伙人类型')" prop="partnerType">
            <dict-select
              v-model="form.partnerType"
              dictionary="partner_type"
              :placeholder="$t('合伙人类型')"
              @change="findCheckFileList"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资者性质')" prop="investorNature">
            <dict-select
              v-model="form.investorNature"
              dictionary="investor_nature"
              :placeholder="$t('投资者性质')"
              @change="findCheckFileList"
            />
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item :label="$t('投资者专业性')" prop="investorMajor">
            <dict-select
              v-model="form.investorMajor"
              dictionary="investor_majors"
              :placeholder="$t('投资者专业性')"
              @change="findCheckFileList"
            />
          </el-form-item>
        </el-col>
        <el-col v-if="form.investorMajor === 'FZYTZZ'" :span="12">
          <el-form-item :label="$t('风险承受能力')" prop="riskTolerance">
            <dict-select
              v-model="form.riskTolerance"
              dictionary="risk_tolerance"
              :placeholder="$t('风险承受能力')"
            />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item :label="$t('备注')">
            <el-input v-model="form.remark" type="textarea" maxlength="2000" :placeholder="$t('备注')" />
          </el-form-item>
        </el-col>
      </el-row>
      <FileTable :list.sync="form.fileList" has-permi="fund:appropriate" data-type="fund_investor_appropriate" :business-id="autoBusinessId($route.query.id, form)" />
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存 取消按钮-->
      <save-btn has-permi="fund:appropriate" />
    </div>
    <!-- 添加或修改客户管理对话框 -->
    <el-dialog
      :title="title"
      :visible.sync="formOpen"
      :close-on-click-modal="false"
      :lock-scroll="false"
      custom-class="fixedDialog"
      width="70%"
      append-to-body
    >
      <lpBaseForm ref="lpBaseForm" @senddata="sendData" />
    </el-dialog>
  </div>
</template>

<script>
    import { saveFundInvestorAppropriate, getFundInvestorAppropriate, checkInvestor } from '@/api/fund/appropriate/fundInvestorAppropriate'
    import { changeNameByType } from '@/api/fund/investor/fundInvestor'
    import lpBaseForm from '@/views/fund/lpBase/lpBaseForm.vue'
    import { findFileListByInvestorNature } from '@/api/fund/verification/fundFileVerification'
    export default {
      components: {
        lpBaseForm
      },
        data() {
            const checkInvestName = (rule, value, callback) => {
              if (value !== undefined && value !== '' && value !== null) {
                checkInvestor(this.$route.query.id, this.form.customerId).then(response => {
                  if (response.data > 0 && (this.form.id === undefined || this.form.id === null || this.form.id === '')) {
                    callback(new Error(this.$t('出资人已存在')))
                  } else {
                    callback()
                  }
                })
              } else {
                callback(new Error(this.$t('必填信息')))
              }
            }
            return {
                // 表单参数
                form: {
                  fileList: []
                },
                // 遮罩层
                loading: false,
                formOpen: false,
                // 表单校验
                rules: {
                  type: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  investName: [{ required: true, validator: checkInvestName, trigger: 'change' }],
                  partnerType: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  investorNature: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }],
                  investorMajor: [{ required: true, message: this.$t('必填信息'), trigger: 'blur' }]
                },
              // 出资人类型
              customerTypeOptions: [],
              // 合伙人类型
              partnerTypeOptions: [],
              // 投资者性质
              investorNatureOptions: [],
              // 投资者专业性
              investorMajorOptions: [],
              // 风险承受能力
              riskToleranceOptions: [],
              // 出资人姓名下拉
              nameOptions: [],
              title: '',
              businessType: ''
            }
        },
        created() {
          this.getDicts('lp_type').then(response => {
            response.data.forEach(item => {
              this.customerTypeOptions.push(item)
            })
            this.getDicts('main_body_type').then(response => {
              response.data.forEach(item => {
                this.customerTypeOptions.push(item)
              })
            })
          })
            this.getDicts('partner_type').then(response => {
                this.partnerTypeOptions = response.data
            })
            this.getDicts('investor_nature').then(response => {
                this.investorNatureOptions = response.data
            })
            this.getDicts('investor_majors').then(response => {
                this.investorMajorOptions = response.data
            })
          this.getDicts('risk_tolerance').then(response => {
            this.riskToleranceOptions = response.data
          })
        },
        methods: {
            // 初始化数据
            init(data) {
                this.loading = true
                if (data != null && data.id !== undefined) {
                    getFundInvestorAppropriate(data.id).then(response => {
                        this.form = response.data
                        this.form.investName = data.userName
                        this.changeNameByTypeToSelect(this.form.type)
                        // 获取需要上传的文档
                        this.findCheckFileList()
                        this.loading = false
                    })
                } else {
                    this.reset()
                    this.loading = false
                }
            },
            // 表单重置
            reset() {
                this.form = {
                    id: undefined,
                    fundId: this.$route.query.id,
                    type: undefined,
                    name: undefined,
                    investName: undefined,
                    customerId: undefined,
                    partnerType: undefined,
                    investorNature: undefined,
                    investorMajor: undefined,
                    riskTolerance: undefined,
                    remark: undefined,
                    createBy: undefined,
                    createTime: undefined,
                    updateBy: undefined,
                    updateTime: undefined,
                    delFlag: undefined,
                    fileList: []

                }
                this.resetForm('form')
            },
            // 取消按钮
            cancel() {
                this.$parent.$parent.formOpen = false
                this.$parent.$parent.reset()
            },
            // 关闭form页面
            closeForm() {
                // 关闭form页面遮罩层
                this.loading = false
                // 关闭form页面
                if (this.$parent.$parent.formOpen !== undefined) {
                  this.$parent.$parent.formOpen = false
                }
                // 刷新list页面
                if (this.$parent.$parent.getList !== undefined) {
                  this.$parent.$parent.getList()
                }
            },
            /** 提交按钮 */
            submitForm() {
              this.$refs['form'].validate(valid => {
                if (valid) {
                  this.loading = true
                  saveFundInvestorAppropriate(this.form).then(response => {
                    if (response.code === 200) {
                      this.msgSuccess(this.$t('保存成功'))
                      this.closeForm()
                    } else {
                      this.msgError(response.msg)
                    }
                    this.loading = false
                  })
                } else {
                  this.locationError(document)
                }
              })
            },
          // 根据类型 改变名称下拉
          changeNameByTypeToSelect: function(type) {
            if (type !== null && type !== undefined && type !== '') {
              changeNameByType(type).then(response => {
                this.nameOptions = response.data
              })
            }
          },
          nameSelect(id) {
            this.form.customerId = id
            if (this.nameOptions.length > 0) {
              this.nameOptions.forEach(item => {
                if (item.id === id) {
                  this.form.investName = item.name
                }
              })
            }
          },
          // 添加新到的客户
          addCustomer() {
            this.formOpen = true
            this.title = this.$t('添加客户信息')
            this.$nextTick(() => {
              this.$refs.lpBaseForm.init(null)
            })
          },
          // 接受子页面传递值
          getList() {
            this.loading = false
          },
          sendData: function(data) {
            this.form.name = data.name
            this.form.type = data.type
            this.form.customerId = data.id
          },
          // 根据投资者性质或许需要上传的文档
          findCheckFileList: function() {
              if (this.form.investorNature && this.form.investorMajor && this.form.partnerType) {
                var fileList = []

                // 去除没有上传文件的附件数据
                for (let i = 0; i < this.form.fileList.length; i++) {
                    if (this.form.fileList[i].name) {
                      fileList.push(this.form.fileList[i])
                    }
                }

                findFileListByInvestorNature(this.form).then(response => {
                  for (let i = 0; i < response.data.length; i++) {
                    var directoryId = response.data[i].directoryId
                    response.data[i].businessId = this.$route.query.id
                    var addFile = true
                    for (let j = 0; j < fileList.length; j++) {
                      if (directoryId === fileList[j].directoryId) {
                        addFile = false
                      }
                    }
                    if (addFile) {
                      fileList.push(response.data[i])
                    }
                  }
                })
                this.form.fileList = fileList
              }
          }
        }
    }
</script>
